package com.hspedu.tankgame3Exercise;

import com.hspedu.tankgame3.Hero;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

/**
 * @author DL5O
 * @version 1.0
 * 画板
 */
public class MyPanel extends JPanel implements KeyListener,Runnable{
    MyTank myTank = null;
    Vector<EnemyTank> enemyTanks = null;
    int enemyNums = 3;

    //进行初始化
    public MyPanel() {
        //初始化自己的坦克，type = 0 为玩家的tank
        myTank = new MyTank(200, 200, 0, 25, 0);
        enemyTanks = new Vector<>();
        //添加敌人的坦克，type = 1 为敌人的坦克
        for (int i = 0; i < enemyNums; i++) {
            EnemyTank enemyTank = new EnemyTank(100 + (80 * i), 100, 2, 30, 1);
            enemyTank.shotSpeed = 10;

            enemyTanks.add(enemyTank);

        }
    }


    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //默认为黑色
        g.fillRect(0, 0, 1000, 750);

        //绘制tank
        //绘制玩家的tank
        drawTank(myTank.getX(),myTank.getY(),myTank.getType(), myTank.getDirect(), g);
        if (myTank.shot != null && myTank.shot.isLive == true){//绘制子弹
            g.draw3DRect(myTank.shot.getX(),myTank.shot.getY(),2,2,false);
        }

        //取出容器中的enemyTank对象，并绘制出来
        for (int i = 0; i < enemyNums ; i++) {
            EnemyTank enemyTank = enemyTanks.get(i);
            //画出坦克
            drawTank(enemyTank.getX(),enemyTank.getY(),1,2,g);
        }

    }

    public void drawTank(int x,int y,int type, int direct, Graphics g) {
        //根据坦克的种类设置tank的颜色，0为玩家的tank，1为敌人的tank
        switch (type) {
            case 0:
                g.setColor(Color.yellow);
                break;
            case 1:
                g.setColor(Color.CYAN);
                break;
            default:
                break;
        }


        //根据坦克的方向来绘制对应形状的tank
        //direct 表示方向，分别用(0 向上 1向右 2向下 3向左)
        switch (direct) {
            case 0://向上的
                g.fill3DRect(x, y, 10, 60, false);//画出左边的轮子
                g.fill3DRect(x + 10, y + 10, 20, 40, false);//中间的矩形
                g.fillOval(x + 10, y + 20, 20, 20);//坦克轮盘
                g.drawLine(x + 20, y + 30, x + 20, y);//坦克的炮筒
                g.fill3DRect(x + 30, y, 10, 60, false);//画出右边的矩形
                break;
            case 1://向右边的
                g.fill3DRect(x, y, 60, 10, false);//画出左边的轮子
                g.fill3DRect(x + 10, y + 10, 40, 20, false);//中间的矩形
                g.fillOval(x + 20, y + 10, 20, 20);//坦克轮盘
                g.drawLine(x + 30, y + 20, x + 60, y + 20);//坦克的炮筒
                g.fill3DRect(x, y + 30, 60, 10, false);//画出右边的矩形
                break;
            case 2://向下
                g.fill3DRect(x, y, 10, 60, false);//画出左边的轮子
                g.fill3DRect(x + 10, y + 10, 20, 40, false);//中间的矩形
                g.fillOval(x + 10, y + 20, 20, 20);//坦克轮盘
                g.drawLine(x + 20, y + 30, x + 20, y + 60);//坦克的炮筒
                g.fill3DRect(x + 30, y, 10, 60, false);//画出右边的矩形
                break;
            case 3://向左
                g.fill3DRect(x, y, 60, 10, false);//画出左边的轮子
                g.fill3DRect(x + 10, y + 10, 40, 20, false);//中间的矩形
                g.fillOval(x + 20, y + 10, 20, 20);//坦克轮盘
                g.drawLine(x + 30, y + 20, x, y + 20);//坦克的炮筒
                g.fill3DRect(x, y + 30, 60, 10, false);//画出右边的矩形
                break;
            default:
                System.out.println("暂时没有处理");
        }
    }

    public void drawShot(Tank tank,Graphics g){
        if(tank instanceof MyTank){

        }
    }

    public void hitTank(Tank tank,Shot shot){

    }


    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_W){
            myTank.moveUp();
            myTank.setDirect(0);
        }else if(e.getKeyCode() == KeyEvent.VK_D){
            myTank.moveRight();
            myTank.setDirect(1);
        }else if(e.getKeyCode() == KeyEvent.VK_S){
            myTank.moveDown();
            myTank.setDirect(2);
        }else if(e.getKeyCode() == KeyEvent.VK_A){
            myTank.moveLeft();
            myTank.setDirect(3);
        }

        if(e.getKeyCode() == KeyEvent.VK_J){
            //用户按下j键，子弹发射,创建shot对象并启动shot线程，
            System.out.println("用户按下j键，子弹发射..");
            myTank.shotEnemy();
        }
        repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {
        while(true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            this.repaint();
        }
    }
}
